var mongoose = require('mongoose');
var User = mongoose.model('User');
var Per = mongoose.model('PerManage');
var Api = mongoose.model('Api');
var Files = mongoose.model('File');
var ApiList = mongoose.model('File');

const date = new Date();
let fun = {
    //创建用户
    getUser: function(res){
        let name = res;
        console.log("创建用户");
        let promise = new mongoose.Promise();
        let user = new User({     
            userName: name,
            createTime: date
        })
        User.create(user,function(err,doc){
            if (err) {
                console.log("error");
                  promise.resolve(err);
              }else{
                var data = {
                    'data': doc
                }
              promise.resolve(err, data);
              }
        })

        return promise;
    },
    //通过名字创建项目
    createProjectByName: async function (pro) {
       console.log("createProjectByName");
        let data = await Per.create(pro,function(err,doc){
            return doc
        })
        return data;
    },
    createApiById: async function (id) {
       console.log("createApiById");
        let data = await Per.create(pro,function(err,doc){
            return doc
        })
        return data;
     },
    getUserById: async function (id) {
       console.log("getUserById");
        let data = await User.create({_id:id},function(err,doc){
            return doc
        })
        return data;
    },
    getProjectById: async function (id) { 
        console.log("getProjectById");
        let data = await Per.findById(id);
        return data;
     },
    getProjectByUserId: async function (id) { 
        console.log("getProjectByUserId");
        let data = await Per.find({userId:id});
        return data;
     },
    createFileByName: async function (file) { 
        console.log("createFileByName");
        let data = await Files.create(file);
        return data;
     },
    getFileByProjectId: async function (id) { 
        console.log("getFileByProjectId");
        let data = await Files.find({proListId:id});
        return data;
     },
    getFileById: async function (id) { 
        console.log("getFileById");
        let data = await Files.findById(id);
        return data;
     },
    allUser: function (){
        
        console.log("查找用户");
        let promise = new mongoose.Promise();
        
        User.find({},function(err,doc){
            if (err) {
                console.log("error");
                  promise.resolve(err);
              }else{
                var data = {
                    'data': doc
                }
              promise.resolve(err, data);
              }
        })
        return promise;
     },
    
}
module.exports = fun;